<!DOCTYPE html>
<html lang="zn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>网络货运平台省运管数据控制台</title>
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="小镖人、社区、网络货运">
    <meta name="description" content="小镖人、社区、网络货运">
    <link rel="stylesheet" href="../../xbr_layui_extend/layui-v2.6.8/css/layui.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="layui-row page-title ">
    <div class="heading-page">
        <img src="../../static/img/数据库.png" height="48px"/>
        <span>网络货运平台省运管数据控制台</span>
    </div>
    <div class="heading-right">
        <button class="layui-btn layui-btn-primary layui-border-blue" id="currentPlat">当前平台：<span>正在获取...</span></button>
    </div>
</div>

<div class="layui-row" style="margin: 0 24px;">
    <blockquote class="layui-elem-quote layui-quote-nm">
        Tips：数据包缺失、出现乱码、上传失败等错误，请及时联系技术人员处理，请勿私自修改数据库数据，以免造成数据包损坏，当前HTML地址：<span id="currentAddress"></span>
    </blockquote>
</div>

<div class="layui-row" style="margin: 0 24px;">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>数据上送配置区</legend>
    </fieldset>
    <div class="layui-form layui-form-pane" action="" lay-filter="form-update">
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">自动上送</label>
            <div class="layui-input-inline">
                <select id="sendInterval" name="sendInterval" lay-filter="sendInterval">
                    <option value="none" selected="">不自动上送</option>
                    <option value="minutes10">间隔10分钟上送</option>
                    <option value="hourOne">间隔1小时上送</option>
                    <option value="dayOne">间隔24小时上送</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">错误提醒</label>
            <div class="layui-input-inline">
                <select id="remindInterval" name="remindInterval" lay-filter="remindInterval">
                    <option value="none" selected="">不提醒</option>
                    <option value="onlyOne">仅在错误时提醒一次</option>
                    <option value="day">间隔24小时提醒一次</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">提醒手机号</label>
            <div class="layui-input-inline">
                <input id="remindPhone" type="text" name="remindPhone" placeholder="请输入提醒手机号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <button class="layui-btn layui-btn-primary layui-border" lay-submit="" lay-filter="component-form-update">更新配置</button>
        </div>
    </div>
</div>
<div class="layui-row" style="margin: 0 24px;">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>数据上送统计</legend>
    </fieldset>
    <div class="layui-form layui-form-pane" action="">
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label" style="width: 120px">上次上送时间</label>
            <div class="layui-input-inline">
                <input type="text" id="lastSentTime" value="--:--" class="layui-input" disabled="">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label" style="width: 120px">上次上送条数</label>
            <div class="layui-input-inline">
                <input type="text" id="lastSentCount" value="--条" class="layui-input" disabled="">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label" style="width: 110px">待上送条数</label>
            <div class="layui-input-inline">
                <input type="text" id="waitSentCount" value="--条" class="layui-input" disabled="">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label" style="width: 100px">失败条数</label>
            <div class="layui-input-inline">
                <input type="text" id="sentFailCount" value="--条" class="layui-input" disabled="">
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label" style="width: 100px">成功条数</label>
            <div class="layui-input-inline">
                <input type="text" id="sentSuccessCount" value="--条" class="layui-input" disabled="">
            </div>
        </div>
    </div>
</div>
<div class="layui-row" style="margin: 0 24px;">
    <table class="layui-hide" id="myTable" lay-filter="myTable"></table>
    <div id="accTablePage"></div>
</div>
<!-- 头部工具栏 -->
<script type="text/html" id="headbar">
   <div>
       <div class="layui-btn-group">
            <a class="layui-btn layui-btn-primary layui-border" lay-event="syncTmsDataList">同步全部数据</a>
            <a class="layui-btn layui-btn-primary layui-border" lay-event="uploadSelect">上送选中记录</a>
            <a class="layui-btn layui-btn-primary layui-border" lay-event="uploadAll">一键上送全部记录</a>
            <a class="layui-btn layui-btn-primary layui-border" lay-event="uploadFail">仅上送失败记录</a>
        </div>
   </div>
    <div class="layui-form layui-form-pane" action="" style="margin-top: 10px;">
        <div class="layui-form-item layui-inline" style="margin: 0;">
            <label class="layui-form-label">文档类型</label>
            <div class="layui-input-inline">
                <select id="ipcType" name="ipcType" lay-filter="ipcType">
                    <option value="">全部</option>
                    <option value="WLHY_YD1001">电子运单</option>
                    <option value="WLHY_YDGC1001">电子运单-挂车补传单</option>
                    <option value="WLHY_ZJ1001">资金流水单</option>
                    <option value="WLHY_CL1001">车辆信息单</option>
                    <option value="WLHY_JSY1001">驾驶员信息单</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="margin: 0;">
            <label class="layui-form-label">数据状态</label>
            <div class="layui-input-inline">
                <select id="status" name="status" lay-filter="status">
                    <option value="transfer">移库待提交</option>
                    <option value="sendSuccess">提交成功</option>
                    <option value="sendFail">提交失败</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="margin: 0;">
            <label class="layui-form-label">数据标识</label>
            <div class="layui-input-inline">
                <input id="identification" type="text" placeholder="请输入唯一数据标识" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn layui-btn-primary layui-border" id="component-query">查询</button>
        </div>
    </div>
</script>

<script type="text/html" id="toolbar">
    <a class="layui-btn layui-btn-sm layui-btn-primary layui-border" lay-event="viewJson">
        <i class="layui-icon layui-icon-fonts-code"></i>
        JSON数据包
    </a>
    {{#  if(d.status != 'sendSuccess'){ }}
    <a class="layui-btn layui-btn-sm layui-btn-primary layui-border" lay-event="upload">
        <i class="layui-icon layui-icon-release"></i>
        上送数据
    </a>
    {{#  } }}
</script>
<script src="../../xbr_layui_extend/layui-v2.6.8/layui.js"></script>
<script>
    layui.extend({
        admin: '/platform/xbr_layui_extend/admin' //集成路径
    }).use(["admin", 'form', 'table','laypage'], function () {
        const admin = layui.admin, table = layui.table, $ = layui.$, form = layui.form,laypage=layui.laypage;
        const statusNames = {
            transfer: "移库待提交",
            sendSuccess: "提交成功",
            sendFail: "提交失败",
        };
        let defIpcType = null, defStatus="transfer";
        let currentLimit = 10,identification;

        initPlat();
        ///检查用户
        function initPlat() {
            $("#currentAddress").html(window.location.href.split("?")[0]);
            const token = admin.getParam("token");
            if (token!=null) admin.saveToken(token);
            admin.req({
                url: "/platform/user/getLoginUserInfo",
                data: {token: token},
                done: function (res) {
                    if (res.data == null) {
                        $("#currentPlat").html("未初始化");
                        return;
                    }
                    $("#currentPlat").text(res.data['organizeName']);
                    initCfg();
                }
            });
        }

        //初始化配置
        function initCfg(){
            admin.req({
                url: "/platform/cfg/initTmsConfigure",
                done: function (res) {
                    if (res.data == null) return;
                    $("#currentPlat").text(res.data['platName']);
                    $("#sendInterval").val(res.data['sendInterval']);
                    $("#remindInterval").val(res.data['remindInterval']);
                    $("#remindPhone").val(res.data['remindPhone']);
                    $("#lastSentTime").val(res.data['lastSentTime']);
                    $("#lastSentCount").val(res.data['lastSentCount'] + "条");
                    $("#waitSentCount").val(res.data['waitSentCount'] + "条");
                    $("#sentFailCount").val(res.data['sentFailCount'] + "条");
                    $("#sentSuccessCount").val(res.data['sentSuccessCount'] + "条");
                    bindUpdate();
                    loadTableData(1);
                }
            });
        }

        //首次登录配置
        $("#currentPlat").click(function(){  // 监听登录按钮
            //弹出页面
            layer.open({
                type: 2,
                title: '登录',
                content: '../user/lverifyCode.html', // 替换为实际页面的 URL
                area: ['600px', '300px'],
                maxmin: true,
                end: function () {   // 弹层关闭后执行回调事件
                    location.reload(); // 刷新当前页面
                }
            });
        });

        //绑定更新表单
        function bindUpdate(){
            form.render(null, 'form-update');
            //提交表单
            form.on('submit(component-form-update)', function (data) {
                admin.req({
                    url:'/platform/cfg/updateTmsConfigure',
                    data: data.field,
                    done: function (res) {
                        layer.msg('更新成功！', {icon: 1, time: 3000});
                    }
                });
                return false;
            });
        }

        //查询 1
        form.on('select(ipcType)', function (data) {
            defIpcType = data.value;
            if(defIpcType==='') defIpcType = null;
            loadTableData(1);
        });

        //查询 2
        form.on('select(status)', function (data) {
            defStatus = data.value;
            loadTableData(1);
        });

        //查询3
        $("body").on("click","#component-query",function () {
            identification = $("#identification").val();
            loadTableData(1);
        });

        ///初始化成功，加载数据
        function loadTableData(page) {
            admin.req({
                url: "/platform/tms/getWaitUploadTmsDataPage",
                data: {ipcType: defIpcType, status: defStatus,identification:identification,page:page,limit:currentLimit},
                done: function (res) {
                    table.render({
                        elem: '#myTable',
                        toolbar: '#headbar', //开启头部工具栏，并为其绑定左侧模板
                        limit: currentLimit,
                        data: res.data.list,
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        cols: [[
                            {type: 'checkbox'},
                            {field: 'id', width: 80, title: 'ID', sort: true},
                            {field: 'platName', title: '平台名称'},
                            {field: 'documentName', title: '文档名称', sort: true},
                            {field: 'documentVersionNumber', title: '文档版本', sort: true},
                            {field: 'identification', title: '数据标识', sort: true},
                            {field: 'ipcType', title: 'ICP类型'},
                            {field: 'status', title: '数据状态', sort: true, templet: function(d){return statusNames[d.status]}},
                            {field: 'autoPostCount', title: '提交次数', sort: true},
                            {field: 'returnMsg', title: '消息'},
                            {fixed: 'right', title: '操作', toolbar: '#toolbar', width: 250}
                        ]],
                        done: function () {
                            $("#ipcType").val(defIpcType);
                            $("#status").val(defStatus);
                            form.render();
                        }
                    });
                    //总页数低于页码总数
                    laypage.render({
                        elem: 'accTablePage',
                        limit: currentLimit,
                        curr: page,
                        limits: [10, 50, 100, 500],
                        layout: ['count','prev', 'page', 'next','limit','refresh','skip'],
                        count: res.data.total, //数据总数
                        jump: function (obj, first) {
                            if (first) return;
                            currentLimit = obj.limit
                            loadTableData(obj.curr)
                        }
                    });
                }
            });
        }

        //头工具栏事件
        table.on('toolbar(myTable)', function (obj) {
            switch (obj.event) {
                case 'syncTmsDataList':
                    syncTmsDataList();
                    break;
                case 'uploadSelect':
                    const checkStatus = table.checkStatus(obj.config.id);
                    const ids = [];
                    checkStatus.data.forEach(function (item) {
                        ids.push(item.id)
                    });
                    postTmsDataByIds(ids);
                    break;
                case 'uploadAll':
                    postAllTmsDataList();
                    break;
                case 'uploadFail':
                    postFailTmsDataList();
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(myTable)', function (obj) {
            const data = obj.data;
            if (obj.event === 'viewJson') {
                layer.open({
                    type: 2,
                    title: data['documentName'] +"-"+data['identification'],
                    scrollbar: true,
                    shadeClose: true,
                    area: ['800px', '600px'],
                    content: ["./open/jsonData.html"],
                    success: function (layer, index) {
                        const iframe = window['layui-layer-iframe' + index];
                        iframe.child(data["jsonContent"]);
                    }
                });
            } else if (obj.event === 'upload') {
                postTmsDataByIds([data.id])
            }
        });

        //同步数据
        function syncTmsDataList(){
            layer.confirm("确认同步数据？", function (index) {
                admin.req({
                    url:'/platform/tms/syncTmsDataList',
                    data: {},
                    done: function (res) {
                        loadTableData(1);
                        layer.msg('同步成功！本次共同步'+res.data+"条数据", {icon: 1, time: 3000});
                    }
                });
                layer.close(index);
            });
        }

        //上送方案1 ids.join(",")
        function postTmsDataByIds(ids){
            layer.confirm('确认上送？共'+ids.length+"条记录", function (index) {
                admin.req({
                    url:'/platform/tms/postTmsDataByIds',
                    data: {ids: ids.join(",")},
                    done: function (res) {
                        loadTableData(1);
                        layer.msg('操作成功！', {icon: 1, time: 3000});
                    }
                });
                layer.close(index);
            });
        }

        //上送方案2
        function postAllTmsDataList(){
            layer.confirm('确认上送？', function (index) {
                admin.req({
                    url:'/platform/tms/postFailTmsDataList',
                    data: data.field,
                    done: function (res) {
                        loadTableData(1);
                        layer.msg('操作成功！', {icon: 1, time: 3000});
                    }
                });
                layer.close(index);
            });
        }

        //上送方案3
        function postFailTmsDataList(){
            layer.confirm('确认上送？', function (index) {
                admin.req({
                    url:'/platform/tms/updateTmsConfigure',
                    data: data.field,
                    done: function (res) {
                        loadTableData(1);
                        layer.msg('操作成功！', {icon: 1, time: 3000});
                    }
                });
                layer.close(index);
            });
        }


    });
</script>
</body>
</html>
